package com.novartis.nims.imagedc.util;

import java.awt.Component;
import java.text.ParseException;

import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;

@SuppressWarnings("serial")
public class FormattedEditor extends DefaultCellEditor {
	
	private JFormattedTextField field;
	
	public FormattedEditor(JFormattedTextField theField) {
		super(theField);
		field = theField;
	}
	
	@Override
	public Object getCellEditorValue() {
		return field.getValue();
	}

	@Override
	public boolean stopCellEditing() {
		if (field.isEditValid()) {
			try {
				field.commitEdit();
			} catch (ParseException e) { }
			
		} else {
			return false;
		}
		
		return super.stopCellEditing();
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		field.setValue(value);
		return field;
	}

}
